package com.suyang.syt.service.impl;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.suyang.syt.service.interfaces.DateBaseBackupService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.PropertiesUtil;

@Service
public class DateBaseBackupServiceImpl implements DateBaseBackupService {
	private static final String BASE_PATH_LINUX = "/home/default/back/database/";
	private static final String BASE_PATH_WINDEWS = "D://";
	private static String url;
	private static String ip;
	private static String dbName;
	private static String username;
	private static String password;
	private static String mysqlPath;

	static {
		try {
			url = PropertiesUtil.get("jdbc.properties", "jdbc.url");
			ip = url.substring(url.indexOf("//") + 2, url.lastIndexOf(":"));
			dbName = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("?"));
			username = PropertiesUtil.get("jdbc.properties", "jdbc.username");
			password = PropertiesUtil.get("jdbc.properties", "jdbc.password");
			mysqlPath = PropertiesUtil.get("jdbc.properties", "mysqlPath");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void mysqlLoad() {
		try {
			Runtime rt = Runtime.getRuntime();
			System.out.println("INFO : " + DateFormat.dateToString(new Date()) + " 当前系统是:Windows->Mysql还原中...");
			Process child = rt.exec(mysqlPath + "mysql -h " + ip + " -u" + username + " -p" + password + " " + dbName);
			OutputStream out = child.getOutputStream();// 控制台的输入信息作为输出流
			String inStr;
			StringBuilder sb = new StringBuilder();
			String outStr;
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(BASE_PATH_WINDEWS + dbName + ".sql"), "utf8"));
			while ((inStr = br.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();
			OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
			writer.write(outStr);
			writer.flush();
			out.close();
			br.close();
			writer.close();
			System.out.println("INFO : " + DateFormat.dateToString(new Date()) + " -->数据库加载成功<--");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void mysqlBackup() throws Exception {
		try {
			Runtime rt = Runtime.getRuntime();
			// 调用 调用mysql的安装目录的命令
			String os = System.getProperty("os.name");
			String mysqlcmd = "mysqldump -h " + ip + " -u" + username + " -p" + password + " " + dbName + " --lock-all-tables=true --default-character-set=utf8";
			Process child = null;
			if (os.toLowerCase().startsWith("win")) {
				mysqlBackupForWindows(child, rt, mysqlcmd);
			} else if (os.toLowerCase().startsWith("lin")) {
				mysqlBackupForLinux(child, rt, mysqlcmd);
			} else {
				System.out.println("Current system is not Window or Linux!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void mysqlBackupForWindows(Process child, Runtime rt, String mysqlcmd) throws Exception {
		System.out.println("INFO : " + DateFormat.dateToString(new Date()) + " 当前系统是:Windows->Mysql备份中...");
		child = rt.exec(mysqlPath + mysqlcmd);
		InputStream in = child.getInputStream();
		InputStreamReader xx = new InputStreamReader(in, "utf-8");
		String inStr;
		StringBuilder sb = new StringBuilder();
		String outStr;
		BufferedReader br = new BufferedReader(xx);
		while ((inStr = br.readLine()) != null) {
			sb.append(inStr + "\r\n");
		}
		outStr = sb.toString();
		String date = DateFormat.dateToString(new Date(), "yyyyMMdd");
		FileOutputStream fout = new FileOutputStream(BASE_PATH_WINDEWS + dbName + date + ".sql");
		OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
		writer.write(outStr);
		writer.flush();
		in.close();
		xx.close();
		br.close();
		writer.close();
		fout.close();
		System.out.println("INFO : " + DateFormat.dateToString(new Date()) + " 备份位置：" + BASE_PATH_WINDEWS + dbName + date + ".sql");
	}

	private void mysqlBackupForLinux(Process child, Runtime rt, String mysqlcmd) throws Exception {
		System.out.println("INFO : " + DateFormat.dateToString(new Date()) + " 当前系统是:Linux->Mysql备份中...");
		String date = DateFormat.dateToString(new Date(), "yyyyMMdd");
		child = rt.exec(new String[] { "sh", "-c", mysqlcmd + ">" + BASE_PATH_LINUX + dbName + date + ".sql" });
		System.out.println("INFO : " + DateFormat.dateToString(new Date()) + " 备份位置：" + BASE_PATH_LINUX + dbName + date + ".sql");
	}

}
